﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace _3D_FileExplorer
{
    class CameraAnimation : DrawableGameComponent
    {
         Vector3 position;
         Vector3 up;
         Vector3 direction;
         Vector3 oldCameraPosition = Fe.camera.cameraPosition;
         Vector3 oldCameraDirection = Fe.camera.cameraDirection;
         Vector3 oldCameraUp = Fe.camera.cameraUp;
         float animationProgress = 0;
         public CameraAnimation(Camera camera,Vector3 target, float distance,Vector3 nodeUp, Game game,bool animate) : base(game)
         {
             if (!animate)
             {
                 animationProgress = 1;
             }
             SetCamera(target, distance, nodeUp);
         }

        public void SetCamera(Vector3 target, float distance,Vector3 nodeUp)
        {
                string axis = FindAxis(nodeUp);
                position = target;

                float shift = distance * 1.2f;
                switch (axis)
                {
                    case "X":
                        position = new Vector3(position.X - shift, position.Y - shift, position.Z - shift);
                        up = new Vector3(0, 0, -1);
                        break;
                    case "-X":
                        position = new Vector3(position.X + shift, position.Y + shift, position.Z + shift);
                        up = new Vector3(0, 1, 0);
                        break;
                    case "Y":
                        position = new Vector3(position.X + shift, position.Y - shift, position.Z - shift);
                        up = new Vector3(0, 0, -1);
                        break;
                    case "-Y":
                        position = new Vector3(position.X - shift, position.Y + shift, position.Z + shift);
                        up = new Vector3(-1, 0, 0);
                        break;
                    case "-Z":
                        position = new Vector3(position.X - shift, position.Y + shift, position.Z + shift);
                        up = new Vector3(0, 1, 0);
                        break;
                    case "Z":
                        position = new Vector3(position.X + shift, position.Y - shift, position.Z - shift);
                        up = new Vector3(1, 0, 0);
                        break;
                }
                direction = target - position;
                direction.Normalize();
        }

        public override void Update(GameTime gameTime)
        {
            if (animationProgress <= 1f)
            {
                Fe.camera.cameraPosition = Vector3.SmoothStep(oldCameraPosition, position, animationProgress);
                Fe.camera.cameraDirection = Vector3.SmoothStep(oldCameraDirection, direction, animationProgress);
                Fe.camera.cameraUp = Vector3.SmoothStep(oldCameraUp, up, animationProgress);
                Fe.camera.view = Matrix.CreateLookAt(Fe.camera.cameraPosition, Fe.camera.cameraPosition + Fe.camera.cameraDirection, Fe.camera.cameraUp);
                animationProgress += Fe.par.DollyInSpeed;
            }
            else
            {
                this.Dispose();
            }
            base.Update(gameTime);

        }
        public string FindAxis(Vector3 Up)
        {
            if (Up.X == 1f)
            {
                return "X";
            }
            if (Up.X == -1f)
            {
                return "-X";
            }
            if (Up.Y == 1f)
            {
                return "Y";
            }
            if (Up.Y == -1f)
            {
                return "-Y";
            }
            if (Up.Z == 1f)
            {
                return "Z";
            }
            if (Up.Z == -1f)
            {
                return "-Z";
            }

            return null;
        }
    }
}
